package cn.jbt.api.admin.event;

import cn.jbt.api.admin.entity.SysLoginLog;
import cn.jbt.api.admin.repository.SysLoginLogRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
@AllArgsConstructor
public class SysLoginLogListener {

    private final SysLoginLogRepository sysLoginLogRepository;

    @Async
    @EventListener(SysLoginLogEvent.class)
    public void log(SysLoginLogEvent event) {
        SysLoginLog sysLoginLog = new SysLoginLog();
        sysLoginLog.setUsername(event.getUsername());
        sysLoginLog.setStatus(event.getStatus());
        sysLoginLog.setMsg(event.getMsg());
        sysLoginLog.setUa(event.getUa());
        sysLoginLog.setIpAddr(event.getIpAddr());
        sysLoginLog.setCreateTime(new Date());
        sysLoginLogRepository.save(sysLoginLog);
    }
}